import axios from 'axios'
import { authRequest, cryptoToken } from '../common/forge-auth'

export const browserDebug = true
const prefix = browserDebug ? '/api' : ''

if (browserDebug) {
  let accessToken = localStorage.getItem('accessToken')
  if (!accessToken) {
    console.log('没有值')
    authRequest()
  } else {
    axios.defaults.headers.common['Authorization'] = 'Bearer ' + cryptoToken(accessToken)
  }
}

/**
 * 获取商品 sku 详情
 * @param {*} id 商品 id
 */
export const getSkuApi = function (id) {
  return axios.get(`${prefix}/shop/sku/${id}`)
}

/**
 * 添加商品进购物车
 * @param {*} data 商品信息
 */
export const cartAddApi = function (data) {
  return axios.post(`${prefix}/shop/cart/add`, data)
}

/**
 * 获取购物车数据
 */
export const cartApi = function () {
  return axios.get(`${prefix}/shop/cart`)
}

/**
 * 账号密码登录接口
 * @param {*} data
 */
export const loginApi = function (data) {
  return axios.post(`/token/passport/login`, data)
}
